package com.ljtg.ljtgservice.service.Impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ljtg.ljtgpojo.dto.CommodityDTO;
import com.ljtg.ljtgpojo.dto.CommodityPageDTO;
import com.ljtg.ljtgpojo.entity.Commodity;
import com.ljtg.ljtgpojo.entity.CommodityDetail;
import com.ljtg.ljtgpojo.vo.CommodityVO;
import com.ljtg.ljtgservice.mapper.CommodityDetailMapper;
import com.ljtg.ljtgservice.mapper.CommodityMapper;
import com.ljtg.ljtgservice.service.CommodityService;
import com.ljtg.result.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    private CommodityMapper commodityMapper;

    @Autowired
    private CommodityDetailMapper commodityDetailMapper;

    @Override
    @Transactional
    public void addCommodity(CommodityDTO commodityDTO) {
        Commodity commodity=new Commodity();
        BeanUtils.copyProperties(commodityDTO,commodity);

        commodityMapper.insert(commodity);

        Long commodityId = commodity.getId();
        List<CommodityDetail> details=commodityDTO.getDetails();
        if (details!=null && details.size()>0){
            details.forEach(detail->{
                detail.setCommodityId(commodityId);
            });
            commodityDetailMapper.insert(details);
        }
    }

    @Override
    public CommodityVO getCommodityById(Long id) {
       Commodity commodity= commodityMapper.selectById(id);
       List<CommodityDetail> details=commodityDetailMapper.getById(id);
       CommodityVO commodityVO=new CommodityVO();
       BeanUtils.copyProperties(commodity,commodityVO);
       commodityVO.setFlavors(details);
       return commodityVO;
    }

    @Override
    public void updateCommodity(CommodityDTO commodityDTO) {
        Commodity commodity=new Commodity();
        BeanUtils.copyProperties(commodityDTO,commodity);

        commodityMapper.updateById(commodity);

        //删除原有的详情数据
        commodityDetailMapper.deleteByCommodityId(commodity.getId());
        //重新添加详情数据
        List<CommodityDetail> details=commodityDTO.getDetails();
        if (details != null && details.size() > 0) {
            //注意：口味数据的dishId前端并不能传递，它是菜单表插入数据后自动生成的主键值，也就是
            //     口味表的关联外键dishId，
            //     有了菜单表这个主键值id，就需要为dishFlavor里面的每一个dishId（关联外键）
            //     属性赋值，所以在批量插入数据之前需要遍历这个对象集合，
            //     为里面的每个对象CommodityDetail的commodityId赋上值
            details.forEach(detail -> {
                detail.setCommodityId(commodity.getId());
            });
            commodityDetailMapper.insert(details);
        }
    }

    public void deleteCommodityById(List<Long> ids) {
        //删除商品表中的详情数据
        for (Long id : ids){
           commodityMapper.deleteById(id);
            //删除商品关联的详情数据
            commodityDetailMapper.deleteByCommodityId(id);
        }
    }

    @Override
    public PageResult pageQuery(CommodityPageDTO commodityPageDTO) {
        PageHelper.startPage(commodityPageDTO.getPage(), commodityPageDTO.getPageSize());
        Page<Commodity> page=commodityMapper.PageQuery(commodityPageDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Commodity> list(Long commodityId) {
        Commodity commodity=new Commodity();
        commodity.setCategoryId(commodityId);
        return commodityMapper.list(commodity);
    }

    @Override
    public List<CommodityVO> listWithdetail(Commodity commodity) {
        List<Commodity> commodityList = commodityMapper.list(commodity);
        List<CommodityVO> commodityVOList=new ArrayList<>();

        for (Commodity c: commodityList){
            CommodityVO commodityVO=new CommodityVO();
            BeanUtils.copyProperties(c,commodityVO);
            List<CommodityDetail> details=commodityDetailMapper.getById(c.getId());

            commodityVO.setFlavors(details);
            commodityVOList.add(commodityVO);
        }

        return commodityVOList;
    }


}